<?php
/**
 * 支付模型
 * @author cjli
 *
 */
class PaymentModel extends Model
{
	//支付插件文件列表
	public static function getPaymentFileList()
	{
		 static $list = array();
		 if(!$list){
			$paymentDir = LIBRARY_PATH . '/COM/Payment';
	    if ($dh = opendir($paymentDir)) {
	    	while(false !== ($file = readdir($dh))) {
	      	if ($file[0] == '.' || $file[0] == '..') {
	        	continue;
	         }
	         $file = ucwords($file);
	         if (is_dir($paymentDir . '/' . $file) && is_file($paymentDir . '/' . $file . '/' . $file . '.info')) {
	         	$info = parse_ini_file($paymentDir . '/' . $file . '/' . $file . '.info');
	         	$info['name'] = $file;
	          	if (!empty($info) && isset($info['title'])) {
	              $list[$file] = $info;
	            }
	          }
	       }
	       closedir($dh);
	     }
		 }
     return $list;
	}
	
	//支付方式列表
	public static function getPaymentList()
	{
		$language_id = C('language_id');
		if(!$language_id) $language_id = 1;
		 $paymentList = self::getPaymentFileList();
     if($paymentList){
     	$payment_db = D('payment');
     	foreach ($paymentList as $pk => &$payment){
     		$payment['status'] = false;
     		$payment_db->field('payment.status,pai.*');
     		$payment_db->join('payment_items AS pai ON pai.payment_name = payment.name');
     		$payment_db->where(array('payment.name' => $pk, 'pai.language_id' => $language_id));
     		$result = $payment_db->find();
     		if($result){
     			$payment['status'] = $result['status'];
     			$payment['title'] = $result['title'];
     			$payment['description'] = $result['description'];
     		}
     	}
     }
    return $paymentList;
	}
	
	/**
   * 获取支付方式信息
   * @param string $paymentname 支付方式标识
   */
	public static function get_payment_info($paymentname)
	{
			$payment_db = D('payment');
			$result = $payment_db->where(array('name' => $paymentname))->find();
     		if($result){
     			foreach ($result as $field => $value){
     				if($field == 'config'){
     					$result[$field] = unserialize($value);
     				}else{
     					$result[$field] = $value;
     				}
     			}
     		}else{
     			$paymentList = self::getPaymentList();
     			return isset($paymentList[$paymentname]) ? $paymentList[$paymentname] : false;
     		}
		return isset($result) ? $result : false;
	}
	
	/**
	 * 获取支付方式详细内容
	 * @param string $payment_name 支付方式标识
	 * @param int $language_id	语言ID
	 */
	public static function getPaymentItems($payment_name, $language_id = null)
	{
		$where['payment_name'] = $payment_name;
		if($language_id){
			$where['language_id'] = $language_id;
		}
		$paymentItems = array();
		$payment_items_db = D('payment_items');
		$items = $payment_items_db->field('language_id,title,description')->where($where)->select();
		if($items){
			foreach ($items as $item){
				$paymentItems[$item['language_id']] = $item;
			}
		}
		return $paymentItems;
	}
	
	/**
	 * 更新支付信息
	 * @param array $post post数据
	 */
	public static function updatePaymentInfo($post)
	{
		$pname = isset($post['name']) ? trim($post['name']) : null;
		if(!$pname) return false;
		
		self::deletePaymentInfo($pname);
		$payment_db = D('payment');
		$data = array(
			'name' => trim($post['name']),
			'poundage_type' => intval($post['poundage_type']),
			'poundage' => (intval($post['poundage_type']) ==1) ? floatval($post['poundage_rate']) : intval($post['poundage_fix']),
			'status' => 1,
			'listorder' => intval($post['listorder']),
			'note' => trim($post['note']),
			'config' => serialize($post['config']),
			'online' => intval($post['online']),
		);
		$lastId = $payment_db->add($data);
		if($lastId){
			$payment_items_db = D('payment_items');
			$languageList = SiteModel::getLanguageList();
			foreach($languageList as $language_id => $language_name){
				if($post['payment'][$language_id]['title']){
					$data = array(
						'payment_name' => $pname,
						'language_id' => $language_id,
						'title' => trim($post['payment'][$language_id]['title']),
						'description' => trim($post['payment'][$language_id]['description']),
						'note' => trim($post['payment'][$language_id]['note']),
					);
					$payment_items_db->add($data);
				}
			}
		}
		self::deletePaymentCache();
		return $lastId;
	}
	
	/**
	 * 删除支付方式信息及内容
	 * @param string $payment_name 支付方式名称
	 */
	public static function deletePaymentInfo($payment_name)
	{
		$payment_db = D('payment');
		$payment_db->where(array('name' => $payment_name))->delete();
		$payment_items_db = D('payment_items');
		$payment_items_db->where(array('payment_name' => $payment_name))->delete();
		self::deletePaymentCache();
	}
	
	/**
	 * 删除支付方式缓存
	 */
	public static function deletePaymentCache()
	{
		$languageList = SiteModel::getLanguageList();
			foreach($languageList as $language_id => $language_name){
				S('payment_list_'.$language_id, NULL);
			}
	}
	
	/**
	 * 获取支付手续费
	 * @param string $paymentname 支付方式名称
	 * @param float $total_fee 支付总费用
	 */
	public static function getPaymeentFee($paymentname, $total_fee)
	{
		$fee = 0;
		$total_fee = floatval($total_fee);
		if($total_fee == 0){
			return $fee;
		}
		$paymentInfo = self::get_payment_info($paymentname);
		if($paymentInfo && $paymentInfo['status'])
		{
			//按百分比收费
			if($paymentInfo['poundage_type'] == 1)
			{
				$fee = $paymentInfo['poundage'] * $total_fee / 100;
			}
			//按固定额度收费
			elseif($paymentInfo['poundage_type'] == 2)
			{
				$fee = $paymentInfo['poundage'];
			}
			return number_format($fee, 2, '.', '');
		}else{
			return $fee;
		}
	}
}